#pragma once

#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void Mydaemon()
{
    //1.忽略信号,管道退出的信号和子进程退出的信号
    signal(SIGPIPE,SIG_IGN);
    signal(SIGCHLD,SIG_IGN);

    //2.创建子进程并退出父进程
    //让子进程成为孤儿进程才能进一步变成守护进程
    if(fork() > 0) exit(0);

    //3.调用setsid
    setsid();

    //4.标准输入、标准输出、标准错误的全部都要向黑洞走
    //守护进程不能有
    int devnull = open("dev/null",O_RDONLY | O_WRONLY);
    if(devnull > 0)
    {
        dup2(devnull,0);
        dup2(devnull,1);
        dup2(devnull,2);
        close(devnull);
    }

}